import axios from 'axios'
import { ElMessage } from 'element-plus'

const service = axios.create({
  baseURL: '/',
  timeout: 5000
})

// 请求拦截器
service.interceptors.request.use(
  config => {
    console.log('Request Config:', config)
    return config
  },
  error => {
    console.error('Request Error:', error)
    return Promise.reject(error)
  }
)

// 响应拦截器
service.interceptors.response.use(
  response => {
    console.log('Response Data:', response.data)
    const { code, msg, data } = response.data

    // 不论成功失败都返回原始响应，让业务代码自己处理
    return response.data
  },
  error => {
    console.error('Response Error:', error)
    ElMessage.error(error?.response?.data?.message || error.message || '请求失败')
    return Promise.reject(error)
  }
)

export default service 